package com.example.cv.mapper;

import com.example.cv.model.Candidate;
import com.example.cv.model.Employer;
import com.example.cv.model.Position;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * @author 周杰
 */
@Mapper
public interface CandidateMapper {
    /**
     * description:求职者查询
     * @param candidate
     * @return com.example.cv.model.Candidate
     */
    @Select("SELECT * FROM tb_candidate WHERE account=#{account} and password=#{password}")
    Candidate findCandidate(Candidate candidate) ;

    /**
     * description:求职者注册（插入）
     * @param candidate
     * @return boolean
     */
    @Insert("insert into tb_candidate values(#{c_id},#{account},#{password},#{name},#{birthday},#{sex},#{school_email},#{phone_number},#{release_time},#{salary_requirement},#{school},#{major},#{personal_evaluation},#{expected_position})")
    boolean addCandidate(Candidate candidate);

    /**
     * description:求职者根据喜好动态查询用人单位
     * @param
     * @return Position
     */
    @SelectProvider(type=CandidateMapperProvider.class,method = "findPositionsByParams")
    List<Position> findPositionsByParams(Position position);

    class CandidateMapperProvider{
        public String findPositionsByParams(Position position){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_position");
                    WHERE("release_time is not null");
                    if(position.getAddress()!=""){
                        AND();
                        WHERE("address=#{address}");
                    }
                    if(position.getPos_name()!=""){
                        AND();
                        WHERE("pos_name=#{pos_name}");
                    }
                    if(position.getSalary()!=""){
                        AND();
                        WHERE("salary=#{salary}");
                    }

                }
            }.toString();
        }
    }

    /**
     * description:查询所有用人单位
     * @param
     * @return java.util.List<com.example.cv.model.Position>
     */
    @Select("select * from tb_position where release_time is not null order by release_time desc")
    List<Position> findAllPositions();

    /**
     * description:根据id查询求职者
     * @param c_id
     * @return com.example.cv.model.Candidate
     */
    @Select("select * from tb_candidate where c_id=#{c_id}")
    Candidate findCandidateById(int c_id);

    /**
     * description:更新求职者发布时间
     * @param candidate
     * @return boolean
     */
    @Update("update tb_candidate set release_time=#{release_time} where c_id=#{c_id}")
    boolean updateCanReleaseTime(Candidate candidate);

    /**
     * description:撤销发布简历
     * @param candidate
     * @return boolean
     */
    @Update("update tb_candidate set release_time=#{release_time} where c_id=#{c_id}")
    boolean deleteCanReleaseTime(Candidate candidate);

    /**
     * description:更新求职者个人信息
     * @param candidate
     * @return boolean
     */
    @Update("update tb_candidate set password=#{password},name=#{name},birthday=#{birthday},sex=#{sex},school_email=#{school_email},phone_number=#{phone_number},salary_requirement=#{salary_requirement},school=#{school},major=#{major},personal_evaluation=#{personal_evaluation},expected_position=#{expected_position}  where c_id=#{c_id}")
    boolean updateCandidate(Candidate candidate);

    @Select("select * from tb_position where pos_name=(select expected_position from tb_candidate where c_id=#{c_id})")
    List<Position> findRecommendPositions(int c_id);

    @Select("select * from tb_position where p_id=#{p_id}")
    Position findPositionByPId(int p_id);

}
